package com.adcc.ags.cgw.entity.user;

import com.adcc.ags.cgw.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * 用户信息
 */
public class UserInfo extends BaseEntity {

    // IP
    private String ip = Strings.nullToEmpty("");

    // 用户ID
    private String id = Strings.nullToEmpty("");

    // 用户名称
    private String name = Strings.nullToEmpty("");

    // 发送时间
    private String sendTime = Strings.nullToEmpty("");

    // 用户状态
    private UserState state = UserState.OFFLINE;

    // 是否为备用用户
    private boolean backup = false;

    /**
     * 构造方法
     * */
    public UserInfo() {}

    /**
     * 构造方法
     * */
    public UserInfo(String id, String name) {
        this.id = id;
        this.name = name;
    }

    @JsonProperty("ip")
    public String getIp() {
        return ip;
    }

    @JsonProperty("ip")
    public void setIp(String ip) {
        this.ip = ip;
    }

    @JsonProperty("id")
    public String getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(String id) {
        this.id = id;
    }

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("sendTime")
    public String getSendTime() {
        return sendTime;
    }

    @JsonProperty("sendTime")
    public void setSendTime(String sendTime) {
        this.sendTime = sendTime;
    }

    @JsonProperty("state")
    public UserState getState() {
        return state;
    }

    @JsonProperty("state")
    public void setState(UserState state) {
        this.state = state;
    }

    @JsonProperty("backup")
    public boolean isBackup() {
        return backup;
    }

    @JsonProperty("backup")
    public void setBackup(boolean backup) {
        this.backup = backup;
    }

    @Override
    public Optional<BaseEntity> formatJson(String json) throws Exception {
        Map<String, String> map = new ObjectMapper().readValue(json, Map.class);
        ip = Strings.nullToEmpty(map.get("ip"));
        id = Strings.nullToEmpty(map.get("id"));
        name = Strings.nullToEmpty(map.get("name"));
        sendTime = Strings.nullToEmpty(map.get("sendTime"));
        return Optional.of((BaseEntity)this);
    }

    @Override
    public String toJSON() throws JsonProcessingException {
        Map<String, String> map = Maps.newHashMapWithExpectedSize(4);
        map.put("ip",ip);
        map.put("id", id);
        map.put("name", name);
        map.put("state", state.name());
        map.put("backup", String.valueOf(backup));
        return new ObjectMapper().writeValueAsString(map);
    }
}
